﻿/**
 * Title:  导航树菜单
 * Description: JS生成树
 * Copyright: Copyright (c) 2004
 * Company: 深圳太极软件公司
 * @author wbli
 * @version 1.0
 */
<!--

//MzTreeView1.0网页树类, 在实例化的时候请把实例名作参数传递进来
function MzTreeView(Tname)
{
  if(typeof(Tname) != "string" || Tname == "")
    throw(new Error(-1, '创建类实例的时候请把类实例的引用变量名传递进来！'));
  
  //【property】
  this.icondefault="";
  this.iconsrc="";
  this.openmodel="";
  this.url      = "#";
  this.target   = "_self";
  this.name     = Tname;
  this.wordLine = false;
  this.currentNode = null;
  this.currentID = "0";
  this.useArrow = true;
  this.nodes = {};
  this.node  = {};
  this.names = "";
  this._d    = "\x0f";
  this.index = 0;
  this.divider   = "_";
  this.node["0"] =
  {
    "id": "0",
    "path": "0",
    "isLoad": false,
    "childNodes": [],
    "childAppend": "",
    "sourceIndex": "0"
  };

  this.colors   =
  {
    "highLight" : "#0A246A",
    "highLightText" : "#FFFFFF",
    "mouseOverBgColor" : "#D4D0C8"
  };
  this.icons    = {
    L0        : 'L0.gif',  //┏
    L1        : 'L1.gif',  //┣
    L2        : 'L2.gif',  //┗
    L3        : 'L3.gif',  //━
    L4        : 'L4.gif',  //┃
    PM0       : 'P0.gif',  //＋┏
    PM1       : 'P1.gif',  //＋┣
    PM2       : 'P2.gif',  //＋┗
    PM3       : 'P3.gif',  //＋━
    empty     : 'L5.gif',     //空白图
    root      : 'root.gif',   //缺省的根节点图标
    folder    : 'folder.gif', //缺省的文件夹图标
    file      : 'file.gif',   //缺省的文件图标
    exit      : 'exit.gif'
  };
  this.iconsExpand = {  //存放节点图片在展开时的对应图片
    PM0       : 'M0.gif',     //－┏
    PM1       : 'M1.gif',     //－┣
    PM2       : 'M2.gif',     //－┗
    PM3       : 'M3.gif',     //－━
    folder    : 'folderopen.gif',

    exit      : 'exit.gif'
  };

  //扩展 document.getElementById(id) 多浏览器兼容性
  //id 要查找的对象 id
  this.getElementById = function(id)
  {
    if (typeof(id) != "string" || id == "") return null;
    if (document.getElementById) return document.getElementById(id);
    if (document.all) return document.all(id);
    try {return eval(id);} catch(e){ return null;}
  }

  //MzTreeView 初始化入口函数
  this.toString = function()
  {
    this.browserCheck();
    this.dataFormat();
    this.load("0");
    var rootCN = this.node["0"].childNodes;
 var str=""; 
    if(rootCN.length>0)
    {
      this.node["0"].hasChild = true;
      for(var i=0; i<rootCN.length; i++)
        str += this.nodeToHTML(rootCN[i], i==rootCN.length-1);		
    }
	return str ;
  };
  
  this.fristclick=function(){
  var temrootCN=this.node["0"].childNodes;
  this.getElementById(this.name+"_tree_"+temrootCN[0].id).click();  
  };
}

//浏览器类型及版本检测
MzTreeView.prototype.browserCheck = function()
{
  var ua = window.navigator.userAgent.toLowerCase(), bname;
  if(/msie/i.test(ua))
  {
    this.navigator = /opera/i.test(ua) ? "opera" : "";
    if(!this.navigator) this.navigator = "msie";
  }
  else if(/gecko/i.test(ua))
  {
    var vendor = window.navigator.vendor.toLowerCase();
    if(vendor == "firefox") this.navigator = "firefox";
    else if(vendor == "netscape") this.navigator = "netscape";
    else if(vendor == "") this.navigator = "mozilla";
  }
  else this.navigator = "msie";
  if(window.opera) this.wordLine = false;
  //取消检验浏览器
  this.navigator = "msie";
  this.wordLine = false;
};


//当根节点为空的时候做的处理
MzTreeView.prototype.atRootIsEmpty = function()
{
  var RCN = this.node["0"].childNodes;
  for(var i=0; i<RCN.length; i++)
  {
    if(!RCN[i].isLoad) this.expand(RCN[i].id);
    if (RCN[i].text=="")
    {
      var node = RCN[i].childNodes[0], HCN  = node.hasChild;
      if(this.wordLine)
      {
        var span = this.getElementById(this.name +"_tree_"+ node.id);
        span = span.childNodes[0].childNodes[0].childNodes[0];
        span.innerHTML = RCN[i].childNodes.length>1 ? "┌" : "─";
      }
      else
      {
        node.iconExpand  =  RCN[i].childNodes.length>1 ? HCN ? "PM0" : "L0" : HCN ? "PM3" : "L3"
        this.getElementById(this.name +"_expand_"+ node.id).src = this.icons[node.iconExpand].src;
      }
    }
  }
};

//初始化数据源里的数据以便后面的快速检索
MzTreeView.prototype.dataFormat = function()
{
  var a = new Array();
  for (var id in this.nodes) a[a.length] = id;
  this.names = a.join(this._d + this._d);
  this.totalNode = a.length; a = null;
};

//在数据源检索所需的数据节点
//id  客户端节点对应的id
MzTreeView.prototype.load = function(id)
{
  var node = this.node[id], d = this.divider, _d = this._d;
  var sid = node.sourceIndex.substr(node.sourceIndex.indexOf(d) + d.length);
  var reg = new RegExp("(^|"+_d+")"+ sid +d+"[^"+_d+d +"]+("+_d+"|$)", "g");
  var cns = this.names.match(reg), tcn = this.node[id].childNodes; 
  if (cns){
  reg = new RegExp(_d, "g"); 
  for (var i=0; i<cns.length; i++)
  tcn[tcn.length] = this.nodeInit(cns[i].replace(reg, ""), id); 
  }
  node.isLoad = true;
};

//初始化节点信息, 根据 this.nodes 数据源生成节点的详细信息
//sourceIndex 数据源中的父子节点组合的字符串 0_1
//parentId    当前树节点在客户端的父节点的 id
MzTreeView.prototype.nodeInit = function(sourceIndex, parentId)
{
  this.index++;
  var source= this.nodes[sourceIndex], d = this.divider;
  var text  = this.getAttribute(source, "text");
  var hint  = this.getAttribute(source, "hint");
  var sid   = sourceIndex.substr(sourceIndex.indexOf(d) + d.length);
  this.node[this.index] =
  {
    "id"    : this.index,
    "text"  : text,
    "hint"  : hint ? hint : text,
    "icon"  : this.getAttribute(source, "icon"),
    "path"  : this.node[parentId].path + d + this.index,
    "isLoad": false,
    "isExpand": false,
    "parentId": parentId,
    "parentNode": this.node[parentId],
    "sourceIndex" : sourceIndex,
    "childAppend" : ""
  };
     this.nodes[sourceIndex] = "index:"+ this.index +";"+ source;
     this.node[this.index].hasChild = this.names.indexOf(this._d + sid + d)>-1;
  if(this.node[this.index].hasChild)  this.node[this.index].childNodes = [];
  return this.node[this.index];
};

//从XML格式字符串里提取信息
//source 数据源里的节点信息字符串(以后可以扩展对XML的支持)
//name   要提取的属性名
MzTreeView.prototype.getAttribute = function(source, name)
{
  var reg = new RegExp("(^|;|\\s)"+ name +"\\s*:\\s*([^;]*)(\\s|;|$)", "i");
  if (reg.test(source)) return RegExp.$2.replace(/[\x0f]/g, ";"); return "";
};

//根据节点的详细信息生成HTML
//node   树在客户端的节点对象
//AtEnd  布尔值  当前要转换的这个节点是否为父节点的子节点集中的最后一项
MzTreeView.prototype.nodeToHTML = function(node, AtEnd)
{
  var source = this.nodes[node.sourceIndex];
  var target = this.getAttribute(source, "target");
  var data = this.getAttribute(source, "data");
  var url  = this.getAttribute(source, "url");
  var icon  = this.iconsrc + (("null" == this.getAttribute(source, "icon") || "" == this.getAttribute(source, "icon"))? this.icondefault : this.getAttribute(source, "icon"));
  var openmodel =this.getAttribute(source, "openmodel");
  if(!url) url = this.url;
  if(data) url += (url.indexOf("?")==-1?"?":"&") + data;
  if(!target) target = this.target;

  var id   = node.id;
  var HCN  = node.hasChild, isRoot = node.parentId=="0";


  var HTML = "";
  if(isRoot)
  HTML="<li onclick=\""+ this.name +".nodeClick('"+ id +"');"+ this.name +".clickHandle("+ id +");this.blur(); \"><a title='' href='#' id='"+ this.name +"_tree_"+ id +"' onclick=\""+ this.name +".nodeClick('"+ id +"');"+ this.name +".clickHandle("+ id +");this.blur(); \"><span>"+node.text+"</span></a></li>";
  if(!isRoot){
	  if(url.toLowerCase().lastIndexOf(".jsp")>0)
		HTML="<dd><a title='' href='javascript:void(0);' onmouseover=\"changeIcon(0,'"+icon+"',this);\" onmouseout=\"changeIcon(1,'"+icon+"',this);\" onclick=\"openSubSys('"+ url +"','"+node.text+"')\" ><img alt='' src='"+icon+"' />"+node.text+"</a></dd>";
	  else if (url.toLowerCase().lastIndexOf(".aspx.tj")>0)
	  {url=url.replace('.aspx.TJ', '.aspx');
	  HTML="<dd><a title='' href='javascript:void(0);' onmouseover=\"changeIcon(0,'"+icon+"',this);\" onmouseout=\"changeIcon(1,'"+icon+"',this);\" onclick=\"openSubSys2('"+ url +"','"+node.text+"')\" ><img alt='' src='"+icon+"' />"+node.text+"</a></dd>";
	  }
	  
	  else if (openmodel=='1')
		HTML="<dd><a title='' href='javascript:void(0);' onmouseover=\"changeIcon(0,'"+icon+"',this);\" onmouseout=\"changeIcon(1,'"+icon+"',this);\" onclick=\"openSubSys('"+ url +"','"+node.text+"')\" ><img alt='' src='"+icon+"' />"+node.text+"</a></dd>";
          else 
		HTML="<dd><a title='' href='javascript:void(0);' onmouseover=\"changeIcon(0,'"+icon+"',this);\" onmouseout=\"changeIcon(1,'"+icon+"',this);\" onclick=\setMainFrameHtml('"+ url +"') ><img alt='"+node.text+"' src='"+icon+"' />"+node.text+"</a></dd>";
	  if(HCN) HTML ="<dt id='"+ this.name +"_tree_"+ id +"' onclick=\""+ this.name +".nodeClick('"+ id +"');"+ this.name +".clickHandle("+ id +") \">"+node.text+"</dt><dd><ul></ul></dd></dd>";
	}
  return HTML;
};


//将某个节点下的所有子节点转化成详细的<HTML>元素表达
//id 树的客户端节点 id
MzTreeView.prototype.buildNode = function(id)
{
  if(this.node[id].hasChild)
  {
    var tcn = this.node[id].childNodes, str = "";
	var tnode=this.node[this.node[id].parentId].childNodes;
    for (var i=0; i<tcn.length; i++)
		str += this.nodeToHTML(tcn[i], i==tcn.length-1);
		if(this.node[id].parentId=="0"){
			var temp = this.getElementById("treeviewarea2");
						
			temp.innerHTML = "<h3>"+this.node[id].text+"</h3><dl class='subdis'>"+str+"</dl>";
			for(var i=0;i<this.node[this.node[id].parentId].childNodes.length;i++){
				this.getElementById(this.name+"_tree_"+tnode[i].id).className="";
			 }
			//alert(22);
			this.getElementById(""+this.name+"_tree_"+id).className="t233";
			//this.getElementById(""+this.name+"_tree_"+id).="on";
			//this.getElementById(""+this.name+"_tree_"+id).style.background='url("images/menu_tab_curr.gif") no-repeat right bottom;color';
			//alert(this.getElementById(""+this.name+"_tree_"+id).style.background);
			//alert(i);
		}else{
			var temp = this.getElementById(this.name +"_tree_"+ id).nextSibling.firstChild;
			if(this.node[id].isExpand==true){			
			temp.style.display="none";
			this.getElementById(this.name +"_tree_"+ id).className="";
			}
			else{			
			temp.innerHTML = str;
			temp.style.display="block";
			this.getElementById(this.name +"_tree_"+ id).className="expand";

			}
			
			
			
	}
  }
};

//点击展开树节点的对应方法
MzTreeView.prototype.expand   = function(id, sureExpand)
{
  var node  = this.node[id];
  if (sureExpand && node.isExpand) return;
  if (!node.hasChild) return;
  var area  = this.getElementById(this.name +"_tree_"+ id);

  if (area)   area = area.childNodes[area.childNodes.length-1];
  if (area)
  {
	 if(!node.isLoad)
	  this.load(id);
	  this.buildNode(id);
	  if(this.node[id].isExpand==true){
		  this.node[id].isExpand=false;
	  }
	  else{ 
	  this.node[id].isExpand=true;
	  }


  }
};

//节点链接单击事件处理方法
//id 客户端树节点的 id
MzTreeView.prototype.nodeClick = function(id)
{
  var source = this.nodes[this.node[id].sourceIndex];
  
  //获取节点在系统中的ID
  var nodeido = this.node[id];
  var sid = nodeido.sourceIndex.substr(nodeido.sourceIndex.indexOf("_") + "_".length); 
  this.currentID = sid;  
  if(this.node[id].hasChild){//存在子节点
    
	  if(!this.node[id].isExpand) {//确认没展开
		  if(document.all("treeid"))  {//当前页面存在treeid
				getId();//获得节点ID
			}

		}else{//确认展开
			if(document.all("treeid"))  {
				getId1();
			}
		}
	}else{//不存在子节点
	if(document.all("treeid"))  {
		getId();//获得节点ID
	}
	}
  eval(this.getAttribute(source, "method"));
 // window.frames[0].location="/app/main2/blank.htm";
  return !(!this.getAttribute(source, "url") && this.url=="#");
};

//为配合系统初始聚集某节点而写的函数, 得到某节点在数据源里的路径
//sourceId 数据源里的节点 id
MzTreeView.prototype.getPath= function(sourceId)
{
  
Array.prototype.indexOf = function(item)
  {
    for(var i=0; i<this.length; i++)
    {
      if(this[i]==item) return i;
    }
    return -1;
  };
  var _d = this._d, d = this.divider;
  var A = new Array(), id=sourceId; A[0] = id;
  while(id!="0" && id!="")
  {
    var str = "(^|"+_d+")([^"+_d+d+"]+"+d+ id +")("+_d+"|$)";
    if (new RegExp(str).test(this.names))
    {
      id = RegExp.$2.substring(0, RegExp.$2.indexOf(d));
      if(A.indexOf(id)>-1) break;
      A[A.length] = id;
    }
    else break;
  }
  return A.reverse();
};


//树的单击事件处理函数
MzTreeView.prototype.clickHandle = function(id)
{
        this.expand(id);
};




//展开当前节点并
MzTreeView.prototype.lowerNode = function()
{
  if (!this.currentNode) this.currentNode = this.node["0"];
  if (this.currentNode.hasChild)
  {
    if (this.currentNode.isExpand)
      this.focusClientNode(this.currentNode.childNodes[0].id);
    else this.expand(this.currentNode.id, true);
  }
}



//本树将要用动的图片的字义及预载函数
//path 图片存放的路径名
MzTreeView.prototype.setIconPath  = function(path)
{
  var k = 0, d = new Date().getTime();
  for(var i in this.icons)
  {
    var tmp = this.icons[i];
    this.icons[i] = new Image();
    this.icons[i].src = path + tmp;
    if(k==9 && (new Date().getTime()-d)>20)
      this.wordLine = true; k++;
  }
  for(var i in this.iconsExpand)
  {
    var tmp = this.iconsExpand[i];
    this.iconsExpand[i]=new Image();
    this.iconsExpand[i].src = path + tmp;
  }
};

//设置树的默认链接
//url 默认链接  若不设置, 其初始值为 #
MzTreeView.prototype.setURL     = function(url){this.url = url;};

//设置树的默认的目标框架名 target
//target 目标框架名  若不设置, 其初始值为 _self
MzTreeView.prototype.setTarget  = function(target){this.target = target;};
// -->

function getId(){	
	
	treeid.value=this.tree.currentID;		
	
}

function getId1(){	
	
	treeid.value="";		
	
}

function setId(tree,treeId){		
for(var i =1;i<=tree.totalNode;i++){
  var nodeido = tree.node[i]; 
  if (nodeido){
  var sid = nodeido.sourceIndex.substr(nodeido.sourceIndex.indexOf("_") + "_".length);  
  if(treeId == sid){
  	tree.currentNode = tree.node[i];
  	if(!tree.node[i].isExpand) {//确认没有展开
  	if(tree.node[i].hasChild) tree.expand(i);//单击事件  	
  	//if(tree.node[i].hasChild) tree.nodeClick(i);
  	//if(!tree.node[i].hasChild) tree.nodeClick(i);
  	}
  }   
  }
}
};


// add by chenwj 20061026 防止重复提交 begin
var allowChangeView = 1;
function do_click_menu(target){
	if (target == 'mainFrame'){
		// 切换锁，防止用户过于频繁的切换 BEGIN
		if (allowChangeView == '0'){
		  alert('系统正忙，请稍等...');
		  return　false;
		}
		allowChangeView = '0';
		setTimeout('allowChangeView=1',1000);
		// 切换锁，防止用户过于频繁的切换 END
	}
	return true;
	
}
// add by chenwj 20061026 防止重复提交 end
function openSubSys(url, name) {
	/*
	var oLink = url;//url +"&sysname=" + encodeURIComponent(name);
	var oWin = window.open(oLink,null,'scrollbars');
	if(document.all) {
		oWin.moveTo(0, 0);
		oWin.resizeTo(window.screen.width, window.screen.height);
		oWin.focus();
	}*/
	document.all.switchFrame.src = url		
	document.all.switchFrame.src = srcHTML;		
}

function setMainFrameHtml(srcHTML){
	document.all.switchFrame.src = "/include/jsp/wait.html"			
	document.all.switchFrame.src = srcHTML;		
}

function changeIcon(flag, iconSrc, obj) {
	obj.firstChild.src = (flag == 0)? iconSrc.substring(0, iconSrc.length-4)+".gif": iconSrc.substring(0, iconSrc.length-4)+".jpg";
	//obj.firstChild.src = iconSrc;
}